package mylab.base.foundation.template.dto.expression;

import com.google.common.base.Preconditions;

import java.util.Arrays;
import java.util.Objects;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * sql操作码
 */
@Getter
@AllArgsConstructor
public enum OP {

    //@formatter:off
    EQ("=",             "等于",       "field = 123"),
    NE("<>",            "不等于",      "field <> 123"),
    GT(">",             "大于",       "field > 123"),
    GE(">=",            "大于等于",     "field >= 123"),
    LT("<",             "小于",        "field < 123"),
    LE("<=",            "小于等于",     "field <= 123"),

    LK("like",          "模糊匹配",     "field like '%值%'"),
    LL("left like",     "左模糊匹配",    "field left like '%值'"),
    LR("right like",    "右模糊匹配",    "field right like '值%'"),
    NL("not like",      "反模糊匹配",    "field not like '%值%'"),

    BT("between",       "区间",         "field between v1 and v2"),
    NB("not between",   "反区间",       "field not between v1 and v2"),

    IN("in",            "包含",         "field in (v1,v2,v3 ...)"),
    NI("not in",        "反包含",       "field not in (v1,v2,v3 ...)"),
    //@formatter:on
    ;

    final String symbol;
    final String desc;
    final String eg;


    public static OP from(String opString) {
        Preconditions.checkNotNull(opString, "操作符不能为空");
        OP result = null;
        try {
            result = Enum.valueOf(OP.class, opString);
        } catch (Exception ignored) {
        }

        if (result != null) {
            return result;
        }

        result = Arrays.stream(OP.values()).filter(op -> Objects.equals(opString, op.getSymbol())).findFirst().orElse(null);
        Preconditions.checkNotNull(opString, "无效的操作符:" + opString);

        return result;
    }

}
